package com.ruoyi.project.applet.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JavaBeanUtil;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.applet.domain.Order;
import com.ruoyi.project.applet.domain.Wallelog;
import com.ruoyi.project.applet.mapper.OrderMapper;
import com.ruoyi.project.applet.mapper.WallelogMapper;
import com.ruoyi.project.applet.service.IOrderService;
import com.ruoyi.project.applet.service.IWallelogService;
import com.ruoyi.project.system.domain.SysUserWx;
import com.ruoyi.project.system.service.ISysUserWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 钱包记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class WallelogServiceImpl implements IWallelogService {
    @Autowired
    private WallelogMapper wallelogMapper;

    @Autowired
    IOrderService orderService;
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ISysUserWxService userWxService;

    /**
     * 查询钱包记录
     *
     * @param id 钱包记录ID
     * @return 钱包记录
     */
    @Override
    public Wallelog selectWallelogById(Long id) {
        return wallelogMapper.selectWallelogById(id);
    }

    @Override
    public Wallelog selectWallelogByTradeNo(String tradeNo) {

        return wallelogMapper.selectWallelogByTradeNo(tradeNo);
    }

    /**
     * 查询钱包记录列表
     *
     * @param wallelog 钱包记录
     * @return 钱包记录
     */
    @Override
    public List<Wallelog> selectWallelogList(Wallelog wallelog) {
        return wallelogMapper.selectWallelogList(wallelog);
    }

    /**
     * 新增钱包记录
     *
     * @param wallelog 钱包记录
     * @return 结果
     */
    @Override
    public int insertWallelog(Wallelog wallelog) {
        wallelog.setCreateTime(DateUtils.getNowDate());
        return wallelogMapper.insertWallelog(wallelog);
    }

    /**
     * 修改钱包记录
     *
     * @param wallelog 钱包记录
     * @return 结果
     */
    @Override
    public int updateWallelog(Wallelog wallelog) {
        return wallelogMapper.updateWallelog(wallelog);
    }

    /**
     * 批量删除钱包记录
     *
     * @param ids 需要删除的钱包记录ID
     * @return 结果
     */
    @Override
    public int deleteWallelogByIds(Long[] ids) {
        return wallelogMapper.deleteWallelogByIds(ids);
    }

    /**
     * 删除钱包记录信息
     *
     * @param id 钱包记录ID
     * @return 结果
     */
    @Override
    public int deleteWallelogById(Long id) {
        return wallelogMapper.deleteWallelogById(id);
    }

    @Override
    public AjaxResult profit(String startTime, String endTime, String status, SysUserWx userWx) {
        Date startDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime);
        Date endDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime);

        Wallelog log = new Wallelog();
        log.setUserId(userWx.getOpenId());
        log.setStatus(status);
        log.setType(Constants.STATUS_NORMAL);
        List<Wallelog> wallelogs = wallelogMapper.selectWallelogList(log).stream().filter(
                a -> a.getCreateTime().after(startDate) && a.getCreateTime().before(endDate)
        ).collect(Collectors.toList());


        Map<String, String> collect = wallelogs.stream().collect(Collectors.toMap(Wallelog::getOrderNumber, Wallelog::getAmount, (a, b) -> b));

        List<Order> orders = null;


        List<String> collect1 = orders.stream().map(Order::getId).collect(Collectors.toList());
        wallelogs = wallelogs.stream().filter(a->collect1.contains(a.getOrderNumber())).collect(Collectors.toList());

        final String plainString = wallelogs.stream()
                .map(a -> new BigDecimal(a.getAmount()))
                .reduce(BigDecimal.ZERO, BigDecimal::add).stripTrailingZeros().toPlainString();
        Map<String, Object> map = new HashMap<>();
        map.put("amount", plainString);
        map.put("orderList", orders.stream().sorted(Comparator.comparing(Order::getCreateTime).reversed()).collect(Collectors.toList()));
        return new AjaxResult(HttpStatus.SUCCESS, "", map);
    }


    @Override
    public AjaxResult take(SysUserWx userWx) {
        Wallelog log = new Wallelog();
        log.setUserId(userWx.getOpenId());
        log.setType(Constants.STATUS_FAULT);
        log.setStatus(Constants.STATUS_FAULT);
        List<Wallelog> wallelogs = wallelogMapper.selectWallelogList(log);
        return new AjaxResult(200, "", wallelogs.stream()
                .map(a -> new BigDecimal(a.getAmount()))
                .reduce(BigDecimal.ZERO, BigDecimal::add).stripTrailingZeros().toPlainString());
    }

    @Override
    public AjaxResult takeLog(SysUserWx userWx) {
        Wallelog log = new Wallelog();
        log.setUserId(userWx.getOpenId());
        log.setType(Constants.STATUS_FAULT);
        List<Wallelog> wallelogs = wallelogMapper.selectWallelogList(log);
        return AjaxResult.success(wallelogs);
    }

}
